Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chapter_hashing): Add js and ts codes for chapter hashing #675

Merged
merged 5 commits into from
Aug 10, 2023

Conversation

yuan0221
Copy link
Contributor

@yuan0221 yuan0221 commented Aug 6, 2023

If this PR is related to coding or code translation, please fill out the checklist and paste the console outputs to the PR.

  • I've tested the code and ensured the outputs are the same as the outputs of reference codes.
  • I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
  • The code does not rely on a particular environment or IDE and can be executed on a standard system (Win, macOS, Ubuntu).

@vercel
Copy link

vercel bot commented Aug 6, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
hello-algo ⬜️ Ignored (Inspect) Visit Preview Aug 9, 2023 3:32pm

Copy link
Contributor

@justin-tse justin-tse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuan0221 Thank you for the clean code, the TS code may follow the JS to address.

const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// 遍历桶,若找到 key 则返回对应 val
for (let pair of bucket) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let pair of bucket) {
for (const pair of bucket) {

const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
for (let pair of bucket) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let pair of bucket) {
for (const pair of bucket) {

Comment on lines 82 to 89
for (let pair of bucket) {
if (pair.key === key) {
this.#buckets[index] = bucket.filter(
(pair) => pair.key !== key
);
this.#size--;
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter method goes through every pair in the bucket twice (once for the for...of loop and once for the filter), splice will be better

Comment on lines 102 to 103
for (let bucket of bucketsTmp) {
for (let pair of bucket) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let bucket of bucketsTmp) {
for (let pair of bucket) {
for (const bucket of bucketsTmp) {
for (const pair of bucket) {


/* 打印哈希表 */
print() {
for (let bucket of this.#buckets) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let bucket of this.#buckets) {
for (const bucket of this.#buckets) {

function addHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (let c of key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let c of key) {
for (const c of key) {

function mulHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (let c of key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let c of key) {
for (const c of key) {

function xorHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (let c of key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let c of key) {
for (const c of key) {

function rotHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (let c of key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let c of key) {
for (const c of key) {

Copy link
Contributor

@justin-tse justin-tse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @yuan0221, there are have 4 positions that need to be updated.

this.#buckets = new Array(this.#capacity).fill(null);
this.#size = 0;
// 将键值对从原哈希表搬运至新哈希表
for (let pair of bucketsTmp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to be updated

@yuan0221
Copy link
Contributor Author

yuan0221 commented Aug 8, 2023

@justin-tse 4 positions have been updated.

Copy link
Contributor

@justin-tse justin-tse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuan0221 Thank you!


/* 打印哈希表 */
print() {
for (let pair of this.#buckets) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to be updated

@krahets krahets changed the title feat(chapter_hashing): Add js and ts codes for chapter 6 feat(chapter_hashing): Add js and ts codes for chapter hashing Aug 8, 2023
Copy link
Contributor

@justin-tse justin-tse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @krahets, I don't have another new review advice. Wait for you to check and merge.

@yuan0221
Copy link
Contributor Author

yuan0221 commented Aug 9, 2023

hi @justin-tse i fixed the bug, and added the js and ts codes for chapter 7.3.
please review,thx.

if (arr[i] !== null) node.right = new TreeNode(arr[i]);
queue.push(node.left, node.right);
} else {
i += 2;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see #678 (comment)

Copy link
Owner

@krahets krahets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yuan0221 . I recommend creating a new PR for the array_binary_tree implementation.

Please remove it and I will merge this one. Thx!

@yuan0221
Copy link
Contributor Author

array_binary_tree

ok, I have removed the array_binary_tree commit, please check and merge

@yuan0221 yuan0221 closed this Aug 10, 2023
@yuan0221 yuan0221 reopened this Aug 10, 2023
@krahets krahets merged commit e97eee0 into krahets:main Aug 10, 2023
1 check passed
@krahets krahets added the code Code-related label Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code Code-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants